/*******************************************************************************
 *
 *      Filename: ViewFrustum.cpp
 *   Description: See ViewFrustum.h
 *        Author: [kb]ypp
 *    Created at: November 7, 2005
 * Last modified: 
 *
 ******************************************************************************/

#include "ViewFrustum.h"
#include "../common/MathUtil.h"

/*ViewFrustum::ViewFrustum()
{
}*/

ViewFrustum::ViewFrustum( const ViewFrustum &vf )
	: fov( vf.fov ), aspect( vf.aspect ), near( vf.near ), far( vf.far )
{
	recalculate();
}

ViewFrustum::VeiwFrusum( float fov, float aspect, float near, float far )
	: fov( fov ), aspect( aspect ), near( near ), far( far )
{
	recalculate();
}

void ViewFrustum::zoom( float factor )
{
	hfov /= factor;
	recalculate();
}

void ViewFrustum::recalculate()
{
	e = 1.0 / tan( deg2rad( 0.5 * aspect ) );
	
	const float ee = e * e;
	const float a = deg2rad( aspect );
	const float aa = a * a;
	const float inv_ee_one = 1.0 / sqrt( ee + 1.0 );
	const float inv_ee_aa = 1.0 / sqrt( ee + aa );
	
	planes[  VF_LEFT_PLANE ].set( vec3(  e*inv_ee_one, 0.0, -inv_ee_one ), 0.0 );
	planes[ VF_RIGHT_PLANE ].set( vec3( -e*inv_ee_one, 0.0, -inv_ee_one ), 0.0 );
	planes[ VF_BOTTOM_PLANE ].set( vec3( 0.0,  e*inv_ee_aa, -a*inv_ee_aa ), 0.0 );
	planes[    VF_TOP_PLANE ].set( vec3( 0.0, -e*inv_ee_aa, -a*inv_ee_aa ), 0.0 );
	planes[ VF_NEAR_PLANE ].set( vec3( 0.0, 0.0, -1.0 ), -near );
	planes[  VF_FAR_PLANE ].set( vec3( 0.0, 0.0,  1.0 ), far );
}
